
import { _decorator, Component, Label, Node, UITransform, v3, Vec3 } from 'cc';
import { RowFallClear_GameData } from '../Data/RowFallClear_GameData';
const { ccclass, property } = _decorator;

enum cardColor {
    CARD_0 = "#969696",
    CARD_1 = "#C8C8C8",
}

@ccclass('RowFallClear_Grid')
export default class RowFallClear_Grid extends Component {
    @property(Label)
    Label_position: Label | null = null;
    //    /** 选中框节点 */
    @property(Node)
    select: Node | null = null;
    @property(Node)
    frame: Node | null = null;
    @property(Node)
    frame_up: Node | null = null;
    @property(Node)
    frame_down: Node | null = null;
    @property(Node)
    frame_left: Node | null = null;
    @property(Node)
    frame_right: Node | null = null;
    id: number = 0;
    x: number = 0;
    y: number = 0;
    //    // 初始化
    init(_id: number, _x: number, _y: number) {
        this.id = _id;
        this.x = _x;
        this.y = _y;

        // const colorIndes = (((this.y + 1) % 2) + ((this.x + 1) % 2)) % 2;
        // this.node.color = new Color().fromHEX(cardColor["CARD_" + colorIndes]);

        this.initWidthAndHeight(RowFallClear_GameData.size, RowFallClear_GameData.size);
        this.initPosition();

        this.Refresh();
        // this.AddListener();
    }
    GetNodeWorldPostation(_node: Node): Vec3 {
        let pos: Vec3 = v3();
        if (_node.parent == null) {
            pos = v3(_node.getPosition());
        } else {
            // pos = v3(_node.parent.convertToWorldSpaceAR(_node.getPosition()));
            pos = _node.getWorldPosition();
        }
        return pos;
    }
    TOUCH_END() {
        // TurkishTetris_GameData.IsMouseDown = false;
    }
    AddListener() {
        this.node.on(
            Node.EventType.TOUCH_END,
            (t) => {
                this.TOUCH_END();
            },
            this
        );
        this.node.on(
            Node.EventType.TOUCH_CANCEL,
            (t) => {
                this.TOUCH_END();
            },
            this
        );

        this.node.on(Node.EventType.TOUCH_MOVE, (t) => { }, this);

        this.node.on(Node.EventType.TOUCH_START, (t) => { }, this);
    }
    Main() { }
    Refresh() {
        this.Label_position.string = `${this.x}-${this.y}`;
        // this.Label_position.node.active = CustomConfigMgr.isDebugEnv();
        this.node.active = this.id >= 0;
    }
    Select(select: boolean): void {
        this.select.active = select;
    }
    //    // 初始化单元宽高
    initWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    //    // 初始化位置
    initPosition() {
        let x = -RowFallClear_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * this.x;
        let y = -RowFallClear_GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * this.y;
        this.node.position = v3(x, y, 0);
    }
}